package com.itheima.app.manager;

import cn.hutool.core.util.StrUtil;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.domain.db.Notification;
import com.itheima.domain.db.Question;
import com.itheima.domain.db.User;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.domain.vo.SettingVo;
import com.itheima.service.db.BlackListService;
import com.itheima.service.db.NotificationService;
import com.itheima.service.db.QuestionService;
import com.itheima.service.db.UserService;
import com.itheima.util.ConstantUtil;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Component//设置的管理代码
public class SettingManager {

    @DubboReference
    private NotificationService notificationService;
    @DubboReference
    private QuestionService questionService;
    @DubboReference
    private UserService userService;
    //用户通用设置 - 读取
    public ResponseEntity findSettings() {
        //1.获得用户id
        Long userId = UserHolder.getUserId();
        //2.查询问题对象
        Question question = questionService.findByUserId(userId);
        //3.查询设置对象
        Notification notification = notificationService.findByUserId(userId);

        //4.封装vo对象
        //4.1 封装用户数据
        SettingVo vo = new SettingVo();
        vo.setId(userId);
        vo.setPhone( UserHolder.get().getMobile() );
        //4.2 封装问题数据
        if(question != null){
            vo.setStrangerQuestion( question.getStrangerQuestion() );
        }else{ //新用户对象
           // vo.setStrangerQuestion( "你喜欢高山还是流水?" ); //给个默认值 别报错
        }


        //4.3 封装通知设置
        if(notification!=null){
            vo.setPinglunNotification( notification.getPinglunNotification() );
            vo.setLikeNotification( notification.getLikeNotification() );
            vo.setGonggaoNotification( notification.getGonggaoNotification() );
        }else{  //新用户
           // vo.setPinglunNotification( false );
           // vo.setLikeNotification( false );
           // vo.setGonggaoNotification( false );
        }
        System.out.println("---------->>>"+vo);
        return ResponseEntity.ok(vo);
    }

    //设置陌生人问题 - 保存
    public ResponseEntity saveOrUpdateQuestions(String content) {
        //1.判断数据库是否存在数据
        Question question = questionService.findByUserId(UserHolder.getUserId());
        //2.根据返回值结果 保存或者更新
        if(question == null ){
            question = new Question();
            question.setUserId( UserHolder.getUserId() ); //根据id修改数据
            question.setStrangerQuestion(content); //内容
            //执行新增
            questionService.save(question);
        }else{//执行修改
            question.setStrangerQuestion(content); //内容
            questionService.update(question);
        }

        return null;
    }

    //保存或者修改通知
    public ResponseEntity saveOrUpdateNotifications(Notification notification) {
        //1.查询数据库
        Notification dbNotification = notificationService.findByUserId(UserHolder.getUserId());
        //2.保存或者更新
        if(dbNotification == null ){
            //保存
            notification = new Notification();
            notification.setUserId( UserHolder.getUserId() ); //设置数据的用户id
            notificationService.save(notification );
        }else{
            //修改
            //notification.setUserId( UserHolder.getUserId() );
            notification.setId( dbNotification.getId() ); //设置数据库的id
            notificationService.update( notification );
        }
        return null;
    }

    @DubboReference
    private BlackListService blackListService;
    //查询黑名单
    public ResponseEntity findBlackList(Integer page, Integer pagesize) {
        //1.获得当前的用户id
        Long loginUserId = UserHolder.getUserId();
        //2.查询黑名单用户
        PageBeanVo pageBeanVo = blackListService.findByPage(page, pagesize, loginUserId);
        return ResponseEntity.ok(pageBeanVo);
    }

    //删除黑名单
    public ResponseEntity deleteBlackList(Long blackUserId) {
        //删除黑名单
        blackListService.delete(UserHolder.getUserId() , blackUserId);
        return null;
    }

    //发送验证码
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    public void sendVerificationCode(String mobile) {
        String code = "123456";
        stringRedisTemplate.opsForValue().set(ConstantUtil.SMS_CODE+mobile,code,5, TimeUnit.MINUTES);
    }


    public Map<String, Boolean> checkVerificationCode(String verificationCode) {
        //获取手机号
        String mobile = UserHolder.get().getMobile();
        //从redis获取验证码
        String rediscode = stringRedisTemplate.opsForValue().get(ConstantUtil.SMS_CODE+mobile);
        boolean verification = true;
        if (!StrUtil.equals(verificationCode,rediscode)){
            verification = false;
            return (Map<String, Boolean>) ResponseEntity.status(500).body("验证码错误");
        }
        //删除redis缓存中的验证码
        stringRedisTemplate.delete(ConstantUtil.SMS_CODE+mobile);

        //通过返回true
        Map<String,Boolean> map = new HashMap<>();
        map.put("verification",verification);
        return map;
    }

    public void updatePhone(Map map, Long userId) {
        String phone = (String) map.get("phone");
        User user = new User();
        user.setId(userId);
        user.setMobile(phone);
        userService.update(user);
    }
}
